﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WB.Library;
using Tongiao.Manager;
using Tongiao.ManagerBase;
using Tongiao.Entity;

public partial class admin_Category_AddCategory : WB.Controls.UcControls
{

    /// <summary>
    /// Loai category : NEWS, PROD
    /// </summary>
    public string CatType
    {
        set
        {
            hidCatType.Value = value;
            switch (hidCatType.Value)
            {
                case "NEWS": lblTitle.Text = "THÊM DANH MỤC TIN TỨC";
                    break;
                case "PROD": lblTitle.Text = "THÊM DANH MỤC SẢN PHẨM";
                    break;

            }
        }
        get { return hidCatType.Value; }
    }

    public Guid CategoryId
    {
        get
        {
            if (FGuid.IsGuid(hidCatId.Value))
                return new Guid(hidCatId.Value);
            return Guid.Empty;
        }
        set
        {
            hidCatId.Value = value.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsPostBack)
        {
            hidCatId.Value = Request["id"];
            if (Request["control"] == "NaddCat")
                CatType = "NEWS";
            else
                CatType = "PROD";
            LoadParent();
            LoadDetail();
            if (CategoryId != Guid.Empty)
            {
                btnUpdate.Visible = true;
            }
            else
                btnUpdate.Visible = false;
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (CategoryId != Guid.Empty)
        {
            Update();
            CustomValidator1.ErrorMessage = "Đã lưu dữ liệu.";
            CustomValidator1.IsValid = false;
        }
        else
        {
            CustomValidator1.ErrorMessage = "Không tìm thấy dữ liệu";
            CustomValidator1.IsValid = false;
        }
    }

    void LoadParent()
    {
        DataTable dtParent = Tg_CategoryManager.CreateInstant().getListParentCat(CatType);
        if (dtParent.Rows.Count > 0)
        {
            ddlParent.Items.Clear();
            ddlParent.Items.Add(new ListItem("--Không chọn--", "00000000-0000-0000-0000-000000000000"));
            ddlParent.AppendDataBoundItems = true;
            ddlParent.DataSource = dtParent;
            ddlParent.DataTextField = "CatName";
            ddlParent.DataValueField = "Id";
            ddlParent.DataGroupField = "CatName";
            ddlParent.DataBind();
        }
    }

    void Insert()
    {
        Tg_CategoryEntity objCat = new Tg_CategoryEntity();
        
        objCat.CatType = CatType.ToString();
        //if (!string.IsNullOrEmpty(ddlParent.SelectedValue.ToString()))
        //{
        //    objCat.Parent =new Guid(ddlParent.SelectedValue);
        //}
        objCat.Parent = new Guid(ddlParent.SelectedValue);
        objCat.CatName = txtCatName.Text;
        objCat.CreatedBy = Profile.UserName;
        objCat.CreatedDate = DateTime.Now;

        Tg_CategoryManager.CreateInstant().Insert(objCat);

    }

    void Update()
    {

        Tg_CategoryEntity objCat = new Tg_CategoryEntity();
        objCat.Id = CategoryId;
        objCat.Parent = new Guid(ddlParent.SelectedValue);
        objCat.CatName = txtCatName.Text;
        objCat.UpdatedBy = Profile.UserName;
        objCat.UpdatedDate = DateTime.Now;

        Tg_CategoryManager.CreateInstant().Update(objCat);

        Response.Redirect(this.Request.RawUrl);

    }

    void LoadDetail()
    {
        if (CategoryId != Guid.Empty)
        {
            DataTable dtCategoryDetail = Tg_CategoryManager.CreateInstant().SelectById(CategoryId);
            if (dtCategoryDetail.Rows.Count > 0)
            {
                if (!string.IsNullOrEmpty(dtCategoryDetail.Rows[0]["Parent"].ToString()))
                    ddlParent.SelectedValue = dtCategoryDetail.Rows[0]["Parent"].ToString();
                txtCatName.Text = dtCategoryDetail.Rows[0]["CatName"].ToString();
            }
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Insert();
        Page_Load(sender, e);
        txtCatName.Text = "";
    }
    protected void btnList_Click(object sender, EventArgs e)
    {
        if (CatType == "NEWS")
            Response.Redirect("?control=NCatList");
        else
            Response.Redirect("?control=ProCatList");
    }
}
